//
//  YLAssessmentProgress.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/19.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLAssessmentProgress.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"

@interface YLAssessmentProgress ()
@property (nonatomic, weak) UILabel *currentLabel;

@property (nonatomic, weak) UILabel *totalLabel;

@property (nonatomic, weak) UIView *progress;

@property (nonatomic, weak) UIView *progressBaseView;
@end


@implementation YLAssessmentProgress

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    UILabel *currentLabel = [[UILabel alloc] init];
    _currentLabel = currentLabel;
    currentLabel.textColor = YLLOrgColor;
    currentLabel.text = @"0";
    [self addSubview:currentLabel];
    [currentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.mas_equalTo(weakSelf);
      //  make.width.mas_equalTo(20);
    }];
    
    UILabel *totalLabel = [[UILabel alloc] init];
    _totalLabel = totalLabel;
    totalLabel.textColor = YLLOrgColor;
    totalLabel.text = @"12";
    [self addSubview:totalLabel];
    [totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.mas_equalTo(weakSelf);
       // make.width.mas_equalTo(20);
    }];
    
    UIView *progressBaseView = [[UIView alloc] init];
    _progressBaseView = progressBaseView;
    [self addSubview:progressBaseView];
    progressBaseView.layer.masksToBounds = YES;
    progressBaseView.layer.cornerRadius = 2;
    [progressBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(currentLabel.mas_right).mas_offset(10);
        make.right.mas_equalTo(totalLabel.mas_left).mas_offset(-10);
        make.centerY.mas_equalTo(weakSelf);
        make.height.mas_equalTo(4);
    }];
    progressBaseView.backgroundColor = [UIColor whiteColor];
    
    UIView *progress = [[UIView alloc] init];
    _progress = progress;
    [weakSelf addSubview:progress];
    progress.layer.masksToBounds = YES;
    progress.layer.cornerRadius = 2;
    [progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(progressBaseView);
        make.centerY.mas_equalTo(weakSelf);
        make.height.mas_equalTo(4);
        make.width.mas_equalTo(progressBaseView).multipliedBy(0.1);
    }];
    progress.backgroundColor = YLLOrgColor;
}

- (void)setCurrentIndex:(NSInteger)currentIndex
{
    _currentIndex = currentIndex;
    __weak typeof(self) weakSelf = self;
    _currentLabel.text = [NSString stringWithFormat:@"%zd", currentIndex];
//    [_progress mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(weakSelf.progressBaseView).multipliedBy();
//    }];
//    
    [_progress mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.progressBaseView);
        make.centerY.mas_equalTo(weakSelf);
        make.height.mas_equalTo(4);
        make.width.mas_equalTo(weakSelf.progressBaseView).multipliedBy(currentIndex / (_totalCount * 1.0));
    }];
}

- (void)setTotalCount:(NSInteger)totalCount
{
    _totalCount = totalCount;
    _totalLabel.text = [NSString stringWithFormat:@"%zd", totalCount];
}

@end
